package io.renren.modules.sys.bottom;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * 下注中奖规则工具类
 *
 * @author wuxianji
 * @since 2017/11/11 上午11:22
 **/
public class BottomRuleUtils {

    /**
     * 判断号码有没有中奖
     * @param winNumber 开奖号码，‘,’号分割，例：1，2，3，4，5
     * @param firstName 下注名称
     * @param number 下注号码
     * @return
     */
    public static Boolean check(String winNumber, String firstName, String number) {

        String[] winNumbers = winNumber.split(",");

        if (firstName.equals("龙虎和")) {
            return checkAllBall(winNumbers, number);
        }
        if (firstName.equals("第一球")) {
            return checkSingleBall(winNumbers[0].trim(), number.trim());
        }
        if (firstName.equals("第二球")) {
            return checkSingleBall(winNumbers[1].trim(), number.trim());
        }
        if (firstName.equals("第三球")) {
            return checkSingleBall(winNumbers[2].trim(), number.trim());
        }
        if (firstName.equals("第四球")) {
            return checkSingleBall(winNumbers[3].trim(), number.trim());
        }
        if (firstName.equals("第五球")) {
            return checkSingleBall(winNumbers[4].trim(), number.trim());
        }
        if (firstName.equals("前三")) {
            return checkThreeBall(winNumbers[0].trim(), winNumbers[1].trim(), winNumbers[2].trim(), number.trim());
        }
        if (firstName.equals("中三")) {
            return checkThreeBall(winNumbers[1].trim(), winNumbers[2].trim(), winNumbers[3].trim(), number.trim());
        }
        if (firstName.equals("后三")) {
            return checkThreeBall(winNumbers[2].trim(), winNumbers[3].trim(), winNumbers[4].trim(), number.trim());
        }
        return false;
    }

    /**
     * 判断龙虎和是否中奖
     * @param winNumbers 开奖号码
     * @param number 购买号码
     * @return
     */
    private static boolean checkAllBall(String[] winNumbers, String number) {
        int sumNumber = 0;
        for (String winNumber : winNumbers) {
            sumNumber += Integer.parseInt(winNumber.trim());
        }
        if (StringUtils.equals(number, "总和大")) {
            return sumNumber >= 23;
        }
        if (StringUtils.equals(number, "总和小")) {
            return sumNumber <=22;
        }
        if (StringUtils.equals(number, "总和单")) {
            return isSingularNumber(sumNumber);
        }
        if (StringUtils.equals(number, "总和双")) {
            return isEvenNumber(sumNumber);
        }

        int firstNumber = Integer.parseInt(winNumbers[0].trim());
        int fiveNumber = Integer.parseInt(winNumbers[4].trim());
        if (StringUtils.equals(number, "龙")) {
            return firstNumber > fiveNumber;
        }
        if (StringUtils.equals(number, "虎")) {
            return firstNumber < fiveNumber;
        }
        if (StringUtils.equals(number, "豹")) {
            return firstNumber == fiveNumber;
        }
        return false;
    }

    /**
     * 判断单球是否中奖
     * @param winNumber 开奖号码
     * @param number 购买号码
     * @return
     */
    private static boolean checkSingleBall(String winNumber, String number) {
        if (StringUtils.equals(number, "单")) {
            return isSingularNumber(Integer.parseInt(winNumber));
        }
        if (StringUtils.equals(number, "双")) {
            return isEvenNumber(Integer.parseInt(winNumber));
        }
        if (StringUtils.equals(number, "大")) {
            return Integer.parseInt(winNumber) >= 5;
        }
        if (StringUtils.equals(number, "小")) {
            return Integer.parseInt(winNumber) <= 4;
        }
        return StringUtils.equals(winNumber, number);
    }

    /**
     * 判断三位号码有没有中奖
     * @param number1 号码1
     * @param number2 号码2
     * @param number3 号码3
     * @param number 下注号码
     * @return
     */
    private static boolean checkThreeBall(String number1, String number2, String number3, String number) {

        List<Integer> list = Lists.newArrayList(Integer.parseInt(number1), Integer.parseInt(number2), Integer.parseInt(number3));

        Collections.sort(list);

        if (StringUtils.equals(number, "豹子")) {
            return isAllSame(list.get(0), list.get(1), list.get(2));
        }
        if (StringUtils.equals(number, "顺子")) {
            return isStraight(list.get(0), list.get(1), list.get(2));
        }
        if (StringUtils.equals(number, "对子")) {
            return isTwoSame(list.get(0), list.get(1), list.get(2));
        }
        if (StringUtils.equals(number, "半顺")) {
            return isHalfStraight(list.get(0), list.get(1), list.get(2));
        }
        if (StringUtils.equals(number, "杂六")) {
            return isOthers(list.get(0), list.get(1), list.get(3));
        }
        return false;
    }

    /**
     * 豹子
     * @param number1
     * @param number2
     * @param number3
     * @return
     */
    private static boolean isAllSame(int number1, int number2, int number3) {
        return number1 == number2 && number2 == number3;
    }

    /**
     * 顺子
     * @param number1
     * @param number2
     * @param number3
     * @return
     */
    private static boolean isStraight(int number1, int number2, int number3) {
        //890 是顺子
        if (number1 == 0 && number2 == 8 && number3 == 9) {
            return true;
        }
        //901是顺子
        if (number1 == 0 && number2 == 1 && number3 == 9) {
            return true;
        }
        return number1 + 1 == number2 && number2 + 1 == number3;
    }

    /**
     * 对子（不包括豹子）
     * @param number1
     * @param number2
     * @param number3
     * @return
     */
    private static boolean isTwoSame(int number1, int number2, int number3) {
        if (isAllSame(number1, number2, number3)) {
            return false;
        }
        return number1 == number2 || number2 == number3;
    }

    /**
     * 半顺（不包括顺子，对子）
     * @param number1
     * @param number2
     * @param number3
     * @return
     */
    private static boolean isHalfStraight(int number1, int number2, int number3) {
        if (isStraight(number1, number2, number3)) {
            return false;
        }
        if (isTwoSame(number1, number2, number3)) {
            return false;
        }
        return number1 + 1 == number2 || number2 + 1 == number3;
    }

    /**
     * 杂六 （什么都不是）
     * @param number1
     * @param number2
     * @param number3
     * @return
     */
    private static boolean isOthers(int number1, int number2, int number3) {
        if (isAllSame(number1, number2, number3)) {
            return false;
        }
        if (isStraight(number1, number2, number3)) {
            return false;
        }
        if (isTwoSame(number1, number2, number3)) {
            return false;
        }
        if (isHalfStraight(number1, number2, number3)) {
            return false;
        }
        return true;
    }

    /**
     * 号码是不是双数
     * @param number
     * @return
     */
    private static boolean isEvenNumber(int number) {
        return number % 2 == 0;
    }

    /**
     * 号码是不是单数
     * @param number
     * @return
     */
    private static boolean isSingularNumber(int number) {
        return number % 2 == 1;
    }

    public static void main(String[] args) {
        String winNumbers = "1,2,3,4,5";
        String firstName = "龙虎和";
        String number = "虎";
        System.err.println("result:" + check(winNumbers, firstName, number));
    }
}
